package com.chenfan.ccp.util;

import com.chenfan.ccp.util.start.ApplicationContextUtil;
import com.chenfan.ccp.utils.TraceIdUtil;
import lombok.Data;
import org.slf4j.MDC;

public class LogUtil {
    public static final String kibnanaUrl = "http://elk.thechenfan.cn/app/kibana#/discover?_g=(time:(from:now-1w,to:now))&_a=(columns:!(traceId,message),interval:auto,query:(language:kuery,query:'%s'),sort:!(!('@timestamp',desc)))";
    public static  String getHttpMsg(String messageTitle,String requestUrI,long costTime){
        // 服务-环境-traceid
        return String.format("【**%s**】%s\n\n【**服务**】%s\n\n【**接口**】%s\n\n【**traceId**】[%s](%s)\n\n【**耗时**】%s",
                ApplicationContextUtil.getProfile(),
                messageTitle,
                ApplicationContextUtil.getServiceName(),
                requestUrI,
                TraceIdUtil.getTraceId(),
                String.format(kibnanaUrl, TraceIdUtil.getTraceId()),
                costTime
        );
    }
    public static  String getSqlMsg(String messageTitle,long costTime){
        // 服务-环境-traceid
        return String.format("【**%s**】%s\n\n【**服务**】%s\n\n【**traceId**】[%s](%s)\n\n【**耗时**】%s",
                ApplicationContextUtil.getProfile(),
                messageTitle,
                ApplicationContextUtil.getServiceName(),
                TraceIdUtil.getTraceId(),
                String.format(kibnanaUrl, TraceIdUtil.getTraceId()),
                costTime
        );
    }
    public static  String getServiceMsg(AlarmMessage alarmMessage){
        // 服务-环境-traceid
        return String.format("" +
                        "【**%s**】" +
                        "%s\n\n" +
                        "【**服务**】%s\n\n" +
                        "【**traceId**】[%s]" +
                        "(%s)\n\n" +
                        "【**异常**】%s",
                ApplicationContextUtil.getProfile(),
                alarmMessage.getTiltle(),
                ApplicationContextUtil.getServiceName(),
                TraceIdUtil.getTraceId(),
                String.format(kibnanaUrl, TraceIdUtil.getTraceId()),
                alarmMessage.getErrorMsg()
        );
    }

    @Data
    public static class AlarmMessage{

        private String tiltle;

        private String errorMsg;

        private String profile;
        private String applicationName;
        private String traceId;
        private String showUrl;
        private String costTime;
        private String requestUri;
    }
}
